limsExperimentalTemplate.chooseMultiPage = {

    /*getGridParam: function () {
        var _this = this, columns = limsExperimentalTemplate.getChooseListColumns()
        _this.param.single ? columns.unshift({
            radio: true
        }) : columns.unshift({
            checkbox: true
        })
        return {
            type: 'grid',
            id: 'lims-experimental-template-choose-list-grid-from',
            url: limsExperimentalTemplate.baseUrl + '/queries/choosable',
            requestData: _this.param,
            columns: columns,
            filterOpen: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    _this.add()
                }
            }],
            onBeforeLoad: function (data) {
                var choosenData
                Gikam.getAsyncComp('lims-experimental-template-choose-list-grid-choosen').done(function (grid) {
                    choosenData = grid.getData()
                })
                if (!Gikam.isEmptyObject(choosenData)) {
                    data = data.filter(function (item) {
                        var flag = true
                        Gikam.each(choosenData, function (i, row) {
                            if (row.id === item.id) {
                                flag = false
                            }
                        })
                        return flag
                    })
                }
                return data
            }
        }
    },*/
    getGridParam : function() {
        var _this = this, columns = limsExperimentalTemplate.getChooseListColumns(), requestData = this.param;
        _this.param.single ? columns.unshift({
            radio : true
        }) : columns.unshift({
            checkbox : true
        });
        requestData.spTestFlag = '1';
        return {
            type : 'grid',
            id : 'lims-experimental-template-choose-list-grid',
            url : limsExperimentalTemplate.baseUrl  + '/queries/choosable',
            requestData : requestData,
            filterOpen:true,
            page:true,
            columns : columns,
            onRowActive : function (index, row) {
                _this.loadSpTestList(row);
            },
            onLoadSuccess : function (rows) {
                if (!Gikam.isEmpty(rows)) {
                    this.activeRowByIndex(0);
                } else {
                    _this.loadSpTestList();
                }
            }
        }
    },

    loadSpTestList : function(row) {
        var _this = this;
        Gikam.getAsyncComp('sp-test-choose-list-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url : limsExperimentalTemplate.etTestUrl + '/queries/raw',
                    requestData : {
                        etId_EQ : row.id
                    },
                    order : {'orderNo':'asc'},
                })
            } else {
                grid.cleanData();
            }
        })
    },

    /*getChoosenGridParam: function () {
        var _this = this, columns = limsExperimentalTemplate.getChooseListColumns()
        _this.param.single ? columns.unshift({
            radio: true
        }) : columns.unshift({
            checkbox: true
        })
        return {
            type: 'grid',
            id: 'lims-experimental-template-choose-list-grid-choosen',
            // url: limsExperimentalTemplate.baseUrl + '/queries/choosable',
            // requestData: _this.param,
            columns: columns,
            filterOpen: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove',
                onClick: function () {
                    _this.remove()
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.CONFIRM',
                icon: 'select',
                onClick: function () {
                    Gikam.getLastModal().close(Gikam.getComp('lims-experimental-template-choose-list-grid-choosen').getData())
                }
            }]
        }
    },

    add: function () {
        var _this = this
        var chooseMultListGrid = Gikam.getComp('lims-experimental-template-choose-list-grid-from')
        var choosenMultListGrid = Gikam.getComp('lims-experimental-template-choose-list-grid-choosen')
        var addUserData = []
        var choosenData = choosenMultListGrid.getData() ? choosenMultListGrid.getData() : [],
            removedRowIndex = chooseMultListGrid.model.$children[0].checkeds

        var chooseData = Gikam.jQuery.map(chooseMultListGrid.getData(), function (rowData, index) {
            if (Gikam.jQuery.inArray(index, removedRowIndex) < 0) {
                return rowData
            }
            addUserData.push(rowData)
        })
        choosenData = choosenData.concat(addUserData)
        _this.arrangeGrid(choosenData)

    },

    remove: function () {
        var _this = this, chooseMultListGrid = Gikam.getComp('lims-experimental-template-choose-list-grid-from'),
            choosenMultListGrid = Gikam.getComp('lims-experimental-template-choose-list-grid-choosen'),
            oldChoosenData = choosenMultListGrid.getData(), delChoosenData = choosenMultListGrid.getSelections(),
            removedRowIndex = choosenMultListGrid.model.$children[0].checkeds

        var choosenData = Gikam.jQuery.map(oldChoosenData, function (rowData, index) {
            if (Gikam.jQuery.inArray(index, removedRowIndex) < 0) {
                return rowData
            }
        })

        var delMessage = Gikam.propI18N('GIKAM.TIP.CONFIRM_TO_DELETE_FOLLOWING_ITEMS') + ':<br/>'
        delChoosenData.forEach(function (row) {
            delMessage = delMessage + row.testCode + '-' + row.testName + '<br/>'
        })
        Gikam.confirm('MODULE.BUSINESS.BATCH.ADD.WAINING', delMessage, function () {
            _this.arrangeGrid(choosenData, delChoosenData)
        })
    },

    arrangeGrid: function (choosenData) {
        var chooseMultListGrid = Gikam.getComp('lims-experimental-template-choose-list-grid-from'),
            choosenMultListGrid = Gikam.getComp('lims-experimental-template-choose-list-grid-choosen')

        choosenMultListGrid.refreshByData({
            rows: choosenData
        })
        chooseMultListGrid.refresh()
        choosenMultListGrid.selectedRowIndex = []

    },*/

    getBtnToolbarParam : function() {
        var _this = this;
        return {
            type : 'btnToolbar',
            items : [ {
                type : 'btnGroup',
                items : [ {
                    type : 'button',
                    text : 'GIKAM.BUTTON.CONFIRM',
                    icon : 'select',
                    onClick : function() {
                        Gikam.getLastModal().close(Gikam.getComp('lims-experimental-template-choose-list-grid').getSelections());
                    }
                }, {
                    type : 'button',
                    text : 'GIKAM.BUTTON.CANCEL',
                    icon : 'cancel',
                    onClick : function() {
                        Gikam.getLastModal().close();
                    }
                } ]
            } ]
        }
    },

    getTabParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'sp-test-choose-list-grid',
            readonly : true,
            columns : limsExperimentalTemplate.getEtTestListColumns1(),
        }
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-experimental-template-choose-multi-list-layout',
            renderTo: Gikam.getLastModal().window.$dom,
            west : {
                width : '50%',
                items : [ this.getBtnToolbarParam(), this.getGridParam() ]
            },
            center : {
                items : [ this.getTabParam() ]
            }
        })
    },

    init: function (param) {
        this.param = param
        this.create()
    }
}
